/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameDanhSachDanhMuc.java
 *
 * Created on Oct 10, 2011, 6:33:28 PM
 */
package desktopapplicationdemo1;

import Database.DanhMuc;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author SEVEN
 */
public class JFrameDanhSachDanhMuc extends javax.swing.JFrame {

    /** Creates new form JFrameDanhSachDanhMuc */
    public JFrameDanhSachDanhMuc() {
        initComponents();
        DienDuLieu();
    }

    private void DienDuLieu()
    {
        DanhMuc dm = new DanhMuc();
        Vector<Object> cols = new Vector<Object>();
        cols.add("Ma Danh Muc");
        cols.add("Ten Danh Muc");
        DefaultTableModel dtm = new DefaultTableModel(dm.DanhSachDanhMuc(), cols){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
            
        };
        this.jTableDSDM.setModel(dtm);
        
        this.jTableDSDM.getTableHeader().setReorderingAllowed(false);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemXoaDM = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDSDM = new javax.swing.JTable();
        jButtonXoa = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldMaDanhMuc = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldTenDM = new javax.swing.JTextField();
        jButtonSave = new javax.swing.JButton();

        jPopupMenu1.setName("jPopupMenu1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplicationdemo1.DesktopApplicationDemo1.class).getContext().getResourceMap(JFrameDanhSachDanhMuc.class);
        jMenuItemXoaDM.setBackground(resourceMap.getColor("jMenuItemXoaDM.background")); // NOI18N
        jMenuItemXoaDM.setForeground(resourceMap.getColor("jMenuItemXoaDM.foreground")); // NOI18N
        jMenuItemXoaDM.setText(resourceMap.getString("jMenuItemXoaDM.text")); // NOI18N
        jMenuItemXoaDM.setName("jMenuItemXoaDM"); // NOI18N
        jMenuItemXoaDM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemXoaDMActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItemXoaDM);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableDSDM.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableDSDM.setComponentPopupMenu(jPopupMenu1);
        jTableDSDM.setName("jTableDSDM"); // NOI18N
        jTableDSDM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDSDMMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableDSDM);

        jButtonXoa.setText(resourceMap.getString("jButtonXoa.text")); // NOI18N
        jButtonXoa.setName("jButtonXoa"); // NOI18N
        jButtonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel1.border.titleFont"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextFieldMaDanhMuc.setEditable(false);
        jTextFieldMaDanhMuc.setText(resourceMap.getString("jTextFieldMaDanhMuc.text")); // NOI18N
        jTextFieldMaDanhMuc.setName("jTextFieldMaDanhMuc"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldTenDM.setName("jTextFieldTenDM"); // NOI18N

        jButtonSave.setText(resourceMap.getString("jButtonSave.text")); // NOI18N
        jButtonSave.setName("jButtonSave"); // NOI18N
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldMaDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonSave)
                            .addComponent(jTextFieldTenDM, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldMaDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldTenDM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonSave)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jButtonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(58, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaActionPerformed
        
        XoaDanhMuc();
               
    }//GEN-LAST:event_jButtonXoaActionPerformed

    private void XoaDanhMuc()
    {
        // Lay gia tri cua ma danh muc duoc chon
        String madm = this.jTableDSDM.getValueAt(this.jTableDSDM.getSelectedRow(), 0).toString();
        int option = JOptionPane.showConfirmDialog(null, "Ban co chac xoa danh muc nay khong?", "Nguy Hiem", JOptionPane.OK_CANCEL_OPTION);
        if(option==JOptionPane.OK_OPTION)
        {
            DanhMuc dm = new DanhMuc();
            dm.setMadm(madm);
            if(dm.XoaDanhMuc())
            {
                JOptionPane.showMessageDialog(null, "Xoa thanh cong");
                DienDuLieu();
            }
            else
                JOptionPane.showMessageDialog(null, "Xoa that bai");
        } 
    }
    
    private void jMenuItemXoaDMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemXoaDMActionPerformed
        
         XoaDanhMuc();
         
    }//GEN-LAST:event_jMenuItemXoaDMActionPerformed

    private void jTableDSDMMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDSDMMouseClicked
        
        String madm = this.jTableDSDM.getValueAt(this.jTableDSDM.getSelectedRow(), 0).toString();
        DanhMuc dm = new DanhMuc();        
        dm.setMadm(madm);
        dm = dm.ThongTinDanhMuc();
        this.jTextFieldMaDanhMuc.setText(dm.getMadm());
        this.jTextFieldTenDM.setText(dm.getTendm());
        
    }//GEN-LAST:event_jTableDSDMMouseClicked

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        
        DanhMuc dm = new DanhMuc();
        dm.setMadm(this.jTextFieldMaDanhMuc.getText());
        dm.setTendm(this.jTextFieldTenDM.getText());
        if(dm.CapNhatDanhMuc())
        {
            JOptionPane.showMessageDialog(null,"Cap nhat thanh cong");
            DienDuLieu();
        }
        else
            JOptionPane.showMessageDialog(null,"Cap nhat that bai");
        
    }//GEN-LAST:event_jButtonSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameDanhSachDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameDanhSachDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameDanhSachDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameDanhSachDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrameDanhSachDanhMuc().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSave;
    private javax.swing.JButton jButtonXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuItem jMenuItemXoaDM;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDSDM;
    private javax.swing.JTextField jTextFieldMaDanhMuc;
    private javax.swing.JTextField jTextFieldTenDM;
    // End of variables declaration//GEN-END:variables
}
